package goodroommate.ui.account;

import java.io.IOException;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import goodroommate.domain.User;
import goodroommate.domain.UserDetail;
import goodroommate.exception.DatabaseException;
import goodroommate.exception.ItemAlreadyExistsException;
import goodroommate.exception.ItemNotExistsException;
import goodroommate.service.IUserDetailService;
import goodroommate.service.impl.UserDetailService;

/**
 * Servlet implementation class DetailInfoServlet
 */
@WebServlet("/Account/ShowUserDetailInfo")
public class ShowUserDetailInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    IUserDetailService userDetailService=new UserDetailService();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowUserDetailInfoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		String strUserID=request.getParameter("userid");
		int userid=0;
		try{
			userid=Integer.parseInt(strUserID);
		}catch(NumberFormatException e){
			
		}
		
//		HttpSession session=request.getSession();
//		User user=(User)session.getAttribute("user");
//		if(user!=null&&user.getId()==userid){
//			
//			response.sendRedirect(request.getContextPath()+"/Account/MyDetailInfoServlet");
//			return;
//		}
		
		
		UserDetail userDetail=null;
		try{
			userDetail=userDetailService.getUserDetailByUserID(userid);
			if(userDetail!=null){
				request.setAttribute("userDetail", userDetail);
				request.getRequestDispatcher("showuserdetailinfo.jsp").forward(request, response);
				return;
			}
		}catch(ItemNotExistsException e){
			request.setAttribute("userDetail", null);
			request.getRequestDispatcher("showuserdetailinfo.jsp").forward(request, response);		
			return;
		}catch(DatabaseException e){
			response.sendError(500);
			return;
		}	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
